<template>
  <div class="app-breadcrumb">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item
        v-for="(item,index) in levelList"
        :key="item.path"
      >
        <span
          v-if="item.redirect==='noRedirect'||index==levelList.length-1"
          class="no-redirect"
        >{{ item.meta.title }}</span>
        <a
          v-else
          @click.prevent="handleLink(item)"
        >{{ item.meta.title }}</a>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <div
      v-if="document"
      class="actions"
    >
      <el-button
        type="text"
        size="mini"
        icon="em-icon-circle-query"
        @click="visible=true"
      >{{ document.name }}使用须知</el-button>
      <el-drawer
        title="操作提示"
        class="adjust-drawer"
        :visible.sync="visible"
      >
        <template slot="title">
          <div class="document-title">
            <h4>{{ document.title }}</h4>
            <div class="document-title__updatetime">更新时间：{{ document.updatedTime }}</div>
          </div>
        </template>
        <el-scrollbar
          style="height:100%"
          wrap-style="overflow-x:hidden;"
        >
          <chmbook :document="document.content" />
        </el-scrollbar>
      </el-drawer>
    </div>
  </div>
</template>

<script>
import pathToRegexp from 'path-to-regexp'
import Chmbook from '@/components/Chmbook'
export default {
  components: {
    Chmbook
  },
  data() {
    return {
      visible: false,
      levelList: null,
      document: null,
      assistTitle: null
    }
  },
  watch: {
    $route(route) {
      if (route.path.startsWith('/redirect/')) {
        return
      }
      this.getBreadcrumb()
    }
  },
  mounted() {
    this.getBreadcrumb()
  },
  methods: {
    getBreadcrumb() {
      const matched = this.$route.matched.filter(item => item.meta && item.meta.title)
      this.levelList = matched.filter(
        item => item.meta && item.meta.title && item.meta.breadcrumb !== false
      )
      // this.onLoadAssist(this.$route.path)
    },
    onLoadAssist(path) {
      this.$http.document.assist(path).then(res => {
        this.document = res.data
      })
    },
    isDashboard(route) {
      const name = route && route.name
      if (!name) {
        return false
      }
      return name.trim().toLocaleLowerCase() === 'Dashboard'.toLocaleLowerCase()
    },
    handleLink(item) {
      const { redirect, path } = item
      if (redirect) {
        this.$router.push(redirect)
        return
      }
      this.$router.push(this.pathCompile(path))
    },
    pathCompile(path) {
      // To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
      const { params } = this.$route
      var toPath = pathToRegexp.compile(path)
      return toPath(params)
    }
  }
}
</script>

<style lang="scss" scoped>
@import 'breadcrumb.scss';
.document-title {
  color: #333;
  &__updatetime {
    color: #999;
  }
}
</style>
